package com.young.mylib.utils;

import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Young on 2017/6/20.
 */

public class FastJsonUtils {
    //  private static final SerializeConfig config;
    private static final String TAG = "FastJsonUtils";

    //    static {
//        config = new SerializeConfig();
//        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
//        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
//
//    }
    public static String getStringValue(JSONObject json, String key) {
        try {
            return json.get(key).toString();
        } catch (Exception e) {
            return "";
        }
    }

    public static JSONArray getArrayValue(JSONObject json, String key) {
        try {
            return toJSONArray(json.get(key).toString());
        } catch (Exception e) {
            Log.i(TAG, "Array is null");
            return null;
        }
    }

    public static String inputStream2String(InputStream input) {
        String jsonStr = "";
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[1024 * 8];
        try {
            for (int n; (n = input.read(b)) != -1; ) {
                String str = new String(b, 0, n);
                Log.i("XXX", str);
                out.append(str);
            }
            jsonStr = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return jsonStr;
    }

    public static JSONObject str2Json(String str) {
        try {
            int indexStart = str.indexOf("{");
            int indexEnd = str.lastIndexOf("}");
            str = str.substring(indexStart, indexEnd + 1);
            return JSONObject.parseObject(str);
        } catch (Exception e) {
            System.out.println("str2json error: " + e.getMessage());
            return null;
        }
    }

    public static <T> JSONObject toJsonObject(T obj) {
        try {
            return (JSONObject) JSON.toJSON(obj);
        } catch (Exception e) {
            return null;
        }
    }

    public static String toJSONString(Object object) {
        try {
            return JSON.toJSONString(object);
        } catch (Exception e) {
            return "";
        }
    }

//    public static Object toBean(String text) {
//        try {
//            return JSON.parse(text);
//        } catch (Exception e) {
//            Log.e(TAG, "toBean error");
//            return null;
//        }
//    }

    public static <T> T toBean(String text, Class<T> clazz) {
        try {
            if (text == null || TextUtils.isEmpty(text)) return null;
            int start = text.indexOf("{");
            int end = text.lastIndexOf("}") + 1;
            text = text.substring(start, end);

            return JSON.parseObject(text, clazz);
//            JSONObject json = JSONObject.parseObject(text);
//            return JSON.toJavaObject(json, clazz);
        } catch (Exception e) {
            Log.e(TAG, "toBean error,return null: " + e.getMessage());
            return null;
        }
    }

    public static <t> List<t> getList(String jsonstring, Class<t> cls) {
        List<t> list = new ArrayList<t>();
        try {
            list = JSON.parseArray(jsonstring, cls);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return list;
    }

    public static <T> T json2bean(String str, Class<T> clz) {
        try {
            JSONArray array = new JSONArray();
            array.add(str);
            return getList(array.toString(), clz).get(0);
        } catch (Exception e) {
            Log.e(TAG, "toBean error,return null");
            return null;
        }
    }

    public static <T> T json2bean(JSONObject json, Class<T> clz) {
        try {
            JSONArray array = new JSONArray();
            array.add(json);
            return getList(array.toString(), clz).get(0);
        } catch (Exception e) {
            Log.e(TAG, "toBean error,return null");
            return null;
        }
    }

    public static JSONArray toJSONArray(String str) {
        try {
            return JSON.parseArray(str);
        } catch (Exception e) {
            Log.e(TAG, "parseArray error: " + str);
            return null;
        }
    }

}
